<template>
	<!-- 不能用v-if (i: 每个tab页的专属下标;  index: 当前tab的下标; 申明在 MescrollMoreItemMixin )-->
	<view class="folder-list" v-show="i === index">
		<!-- top="120"下拉布局往下偏移,防止被悬浮菜单遮住 -->
		<mescroll-uni ref="mescrollRef" @init="mescrollInit" top="0" bottom="0" :height="scrollHeight" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback" @emptyclick="emptyClick">
			<!-- 数据列表 -->
			<good-list ref="goodList" :list="goods" :showCheckbox="showCheckbox" :footerBotttom="footerBotttom" @lockDownScroll="lockDownScroll" @triggerDownScroll="triggerDownScroll" @showPopup="showPopup"></good-list>
		</mescroll-uni>
		<!-- 操作菜单 -->
		<uni-popup ref="popupTheme" type="center" :animation="true">
			<view class="popupTheme theme-bg-auto2">
				<view class="Themetitle lz-ellipsis-1">{{popup.title}}</view>
				<view class="Themecenter">
						<!-- 
						fileType：0为文件夹1word 2Excel 3其他
						roleType//0是管理者权限，1为上传/编辑权限，2为查看权限 
						filingType//0共享盘1私人盘2加密盘(加密盘不需要在线编辑，转发副本，只有查看和上传功能)
						-->
					<view class="listItem" v-if="menu.data&&menu.data.fileType!=0&&menu.data.filingType!=2" @click="onlineEditFn">在线编辑</view>
					<view class="listItem" v-if="menu.data&&menu.data.fileType!=0&&menu.data.filingType!=2&&menu.data.filingCabinetsRole&&menu.data.filingCabinetsRole.roleType!='1'" @click="forwardFn">转发副本</view>
					<view class="listItem" @click="newNameFn">重命名</view>
					<view class="listItem" @click="deleteFile()">删除</view>
				</view>
				<view class="Themefoot" @click="$refs.popupTheme.close()">取 消</view>
			</view>
		</uni-popup>
		<!-- 对话框 -->
		<uni-popup ref="dialogInput" type="dialog">
			<uni-popup-dialog mode="input" :title="menu.data&&menu.data.filingName" subTitle="请输入新命名" placeholder="请输入新命名"
			 :value="menu.data&&menu.data.filingName" type="error" @confirm="dialogInputConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	let that,mescroll
	import {
		mapGetters,
		mapActions
	} from 'vuex'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import MescrollMoreItemMixin from "@/components/mescroll-uni/mixins/mescroll-more-item.js";
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
	import goodList from '../good-list/good-list.vue'
	export default {
		mixins: [MescrollMixin,MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		computed:{
			...mapGetters(['APPTheme', 'userInfo', 'api']),
		},
		components:{
			goodList,
			uniPopupDialog
		},
		props:{
			i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			index: { // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
				type: Number,
				default(){
					return 0
				}
			},
			otherData:{//请求的其他参数
				type: Object,
				default(){
					return {
						parentId:'',//当前文件夹id
						filingType:'',//0共享盘1私人盘2加密盘
						filingName:'',//搜索文件名
						search:false,//当前是否为搜索
					}
				}
			},
			//是否开启长按多选功能
			showCheckbox: {
				type: Boolean,
				default: false
			},
			//局部滚动高度
			scrollHeight: {
				type: String,
				default: '100%'
			},
			//底部操作菜单位置
			footerBotttom: {
				type: String,
				default: '0px'
			},
		},
		data() {
			return {
				//下拉配置
				downOption: {
					use: true, // 下拉
					autoShowLoading: true, // 显示下拉刷新的进度条
					textColor: "#FF8095" // 下拉刷新的文本颜色
				},
				//上拉配置
				upOption: {
					use: true, // 上拉
					empty: {
						use: true,
						icon: '',
						tip: '暂无数据'
					},
					toTop: {
						src: '' // 不显示回到顶部按钮
					}
				},
				goods: [] ,//列表数据
				
				//弹出层
				popup: {
					title: ''
				},
				
				//点击菜单
				menu: {
					data: uni.getStorageSync('folder_menu_data') || null, //当前点击行参数
				},
				//转发副本
				forward: {
					employeesList: [], //已选择人
					departmentsList: [], //已选择组织
					roleList: [], //已选择的角色
				},
				
			}
		},
		watch:{
			'otherData.filingName':(res)=>{
				console.log('监听变化',res);
				//主动启动下拉刷新
				that.triggerDownScroll()
			}
		},
		mounted() {
			that=this
			//监听到已选转发人
			uni.$off('mail_confirm')
			uni.$on('mail_confirm', (res) => {
				console.log('监听到转发人',res);
				that.forward = res
				that.menu.data=uni.getStorageSync('folder_menu_data')
				that.forWordFile()
			})
			//监听在线编辑保存成功
			uni.$off('sever_Wps')
			uni.$on('sever_Wps',(res)=>{
				setTimeout(()=>{
					uni.showToast({
						title: '编辑保存成功'
					});
				},500)
			})
		},
		methods: {
			lockDownScroll(onoff){
				console.log(onoff);
				//onoff true禁用下拉刷新，false启用
				this.mescroll.lockDownScroll(onoff)
			},
			//主动启动下拉刷新
			triggerDownScroll(){
				this.mescroll.triggerDownScroll()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				// this.index: 当前tab的下标
				//获取数据
				this.filingCabinets_list(page)
			},
			//点击空布局按钮的回调
			emptyClick(){
				uni.showToast({
					title:'点击了按钮,具体逻辑自行实现'
				})
			},
			//获取数据
			filingCabinets_list(page){
				let that=this,req,api=''
				if(that.otherData.search){//当前为搜索
					req={
						parentId:that.otherData.id,//当前文件夹id
						filingType:that.otherData.filingType,//0共享盘1私人盘2加密盘
						filingName:that.otherData.filingName||'',//搜索名
						pageNo:page.num,
						pageSize:page.size,
					}
					api=that.api.GLOBAL_admin_rest_biz_filingCabinets_searchPage
				}else{//当前为普通获取
					req={
						parentId:that.otherData.id,//当前文件夹id
						filingType:that.otherData.filingType,//0共享盘1私人盘2加密盘
						// filingName:that.otherData.filingName||'',//普通获取不穿搜索的filingName
						pageNo:page.num,
						pageSize:page.size,
					}
					if(that.otherData.filingType==0){//共享盘
						api=that.api.GLOBAL_admin_rest_biz_filingCabinets_list
					}else{
						api=that.api.GLOBAL_admin_rest_biz_filingCabinets_searchPage
					}
				}
				that.GLOBAL.doGet(api,req).then(res=>{
					if(res.code=='0'){
						//判断是否有下一页
						let onoff=that.isNextPage(page,res)
						//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
						that.mescroll.endSuccess(res.data.length,onoff);
						//设置列表数据
						if(page.num == 1) that.goods = []; //如果是第一页需手动制空列表
						that.goods=that.goods.concat(res.data)
						console.log(that.goods);
					}
				}).catch((err)=>{
					//联网失败, 结束加载
					that.mescroll.endErr();
				})
			},
			//判断是否有下一页
			isNextPage(page,res){
				let num =page.num*page.size
				return num<res.count?true:false
			},
			//点击菜单
			showPopup(data){
				let that=this
				console.log('点击菜单',data);
				that.popup.title=data.filingName
				// roleType//0是管理者权限，1为上传/编辑权限，2为查看权限
				if(data.filingCabinetsRole.roleType!='2'){
					that.menu.data = data
					//存储下当前点击的信息，否则$on函数触发后that.menu.data值会还原【组件中存在的现象】
					uni.setStorageSync('folder_menu_data', data)
					that.$refs.popupTheme.open()
				}else{
					uni.showToast({
						title: '您没有权限操作此菜单',
						icon:"none"
					});
				}
				
			},
			//在线编辑
			onlineEditFn() {
				let that = this
				console.log(that.menu.data);
				let req = {
					_w_fname: that.menu.data.filingName+'.'+that.menu.data.suffix, //文件名
					parentId: that.menu.data.parentId, //父文件夹id
					filingType: that.menu.data.filingType, //文件柜类型
					roleType:that.menu.data.filingCabinetsRole.roleType,//0是管理者权限，1为上传/编辑权限，2为查看权限
					fId:that.menu.data.id,//当前文件id
					status:1,//新建时0，在线编辑1
				}
				that.GLOBAL.doGet(that.api.GLOBAL_v1_api_file_getUrl, req).then(res => {
					console.log(res);
					if (res.code == '0') {
						let wps = {
							wpsUrl: encodeURIComponent(res.data.wpsUrl),
							token: res.data.token,
						}
						uni.navigateTo({
							url: `/pages/document/WPS/WPS?wpsUrl=${wps.wpsUrl}&token=${wps.token}`,
							success: res => {},
							fail: () => {},
							complete: () => {}
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				}).catch(err=>{
					console.log(err);
				})
			},
			//删除一个文件及文件夹
			deleteFile() {
				let that = this
				that.$refs.popupTheme.close()
				//0是管理者权限，1为上传/编辑权限，2为查看权限
				if(that.menu.data.filingCabinetsRole.roleType=='1'&&that.menu.data.user.id!=JSON.parse(that.userInfo).id){
					uni.showToast({
						title: '您没有权限删除此文件',
						icon:"none"
					});
					return
				}
				uni.showModal({
					title: '提示',
					content: '确认将文件移入到回收站？',
					success: function(res) {
						if (res.confirm) {
							let	req = {
									ids: that.menu.data.id, //文件名
								}
							that.GLOBAL.doGet(that.api.GLOBAL_admin_rest_biz_filingCabinets_delete, req).then(res => {
								if (res.code == '0') {
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									});
									//主动启动下拉刷新
									that.triggerDownScroll()
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			
			},
			//重命名
			newNameFn() {
				let that = this
				that.$refs.popupTheme.close()
				that.$refs.dialogInput.open()
			},
			//点击确认重命名
			dialogInputConfirm(fn, item) {
				let that = this
				console.log(item);
				let req = {
					id: that.menu.data.id,
					name: item
				}
				that.GLOBAL.doPost(that.api.GLOBAL_admin_rest_biz_filingCabinets_newName, req).then(res => {
					that.$refs.dialogInput.close()
					if (res.code == '0' && res.data.flag) {
						uni.showToast({
							title: res.data.msg,
							icon: "success"
						});
						//主动启动下拉刷新
						that.triggerDownScroll()
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: "none"
						});
					}
				})
			
			},
			//转发副本
			forwardFn() {
				let that = this
				that.$refs.popupTheme.close()
				let data = {
					employeesList: [], //已选择人
					departmentsList: [], //已选择组织
					roleList: [], //已选择的角色
				}
				uni.navigateTo({
					url: '/pages/workbench/selectionMethod/selectionMethod?title=转发人&data=' + JSON.stringify(data)
				});
			
			},
			//转发确认
			forWordFile() {
				let that = this
				console.log(that.menu.data);
				let req = {
					userId: that.forward.employeesList[0].id, //人id
					id: that.menu.data.id //文件id
				}
				that.GLOBAL.doPost(that.api.GLOBAL_admin_rest_biz_filingCabinets_forWordFile, req).then(res => {
					if (res.code == '0') {
						uni.showToast({
							title: res.data.msg,
							icon:"none"
						});
					}
				})
			},
			
		}
	}
</script>
<style scoped lang="scss">
	@import '../good-list/good-list.scss';
</style>
